package com.yang.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.yang.entity.User;
import com.yang.util.DBHelp;
import com.yang.util.MapRow;

public class UserDao {

	private DBHelp<User> dh=new DBHelp<User>();
	public User findByNameAndPassword(String name, String password) {
		String sql=null;
		MapRow<User> mr=new Mr();
		if(password==null){
			sql="select * from t_user where `name`=?";
			return dh.findObj(sql, mr, name);
		}else{
			sql="select * from t_user where `name`=? and `password`=?";
			return dh.findObj(sql, mr, name,password);
		}
	}
	private class Mr implements MapRow<User>{

		@Override
		public User RowMap(ResultSet rs) throws SQLException {
			User u=new User();
			u.setId(rs.getString("id"));
			u.setName(rs.getString("name"));
			u.setPassword(rs.getString("password"));
			return u;
		}
		
	}
	public User insertByNameAndPassword(String id, String name, String password) {
		
		String sql="insert into t_user(id,name,password)values(?,?,?)";
		boolean b=dh.add(sql, id,name,password);
		if(b){
			return findByNameAndPassword(name, password);
		}
		return null;
	}

}
